<?php 
/*
IMPORTANTE: UTILIZA IMAGECREATE EN LUGAR DE IMAGETRUECOLOR

$sizes=array(381,536);
$forze="bouth";
$margin=10;
generate_scaled_image("image/imagen.jpg",$sizes,$forze,$margin,"thumb/");
*/
function add_new_message($v,$g){
	
}
function hex2rgb($hexVal = "") {
	$hexVal = eregi_replace("[^a-fA-F0-9]", "", $hexVal);
	if (strlen($hexVal) != 6) { return "ERROR: Incorrect colorcode, expecting 6 chars (a-f, 0-9)"; }
	$arrTmp = explode(" ", chunk_split($hexVal, 2, " "));
	$arrTmp = array_map("hexdec", $arrTmp);
	return array("red" => $arrTmp[0], "green" => $arrTmp[1], "blue" => $arrTmp[2]);
}

/**
* Document::generate_scaled_image()
*
* { Description }
*
* @param [string] $image_path :: path/image.jpg
* @param [array] $sizes :: array(width,height) canvas
* @param [string] $forze :: width, height, bouth
* @param [array] $margin :: array(width,height)
* @return  bool
*/

function generate_scaled_image($image_path,$sizes,$forze,$margin,$savepath){
	//error_reporting(1);
	add_new_message("img scale para: $image_path",'log');
	$file_name=get_filename($image_path);
	add_new_message("img name: $file_name",'log');
	
	$ext=get_file_extension($image_path);
	add_new_message("img ext: $ext",'log');
	
	$image_file=$file_name.".".$ext;
	add_new_message("img comp: $image_file",'log');
	
	
	$final3=false;
	
	list($owidth, $oheight) = getimagesize($image_path);
	$nwidth =$sizes[0];
	$nheigth=$sizes[1];
	// Cargar la imagen
	
	
	if($ext==false)	{
		return false;
	}else if($ext=="jpg" || $ext=="jpeg"){
		$source = imagecreatefromjpeg($image_path);
		if(!$source){
			add_new_message("Error creating a JPG image",'error-log');
			return false;
		}
	}else if($ext=="gif"){
		$source = imagecreatefromgif($image_path);
	}else if($ext=="png"){
		$source = imagecreatefrompng($image_path);
	}else{
		add_new_message("No esta definido el control para imagenes: ".$ext,'error-log');
		return false;
	}
	// forzar la imagen a encajar en el ancho
	if($forze=='width'){
		$percentage=ceil(($nwidth*100)/$owidth);
		$heigth_canv=ceil(($oheight*$percentage)/100);
		
		$imagen1 = imagecreate($nwidth, $heigth_canv);
		imagecopyresized($imagen1, $source, 0, 0, 0, 0, $nwidth, $heigth_canv, $owidth, $oheight);
		$image_final=$imagen1;
		
	}else if($forze=='height'){
		$percentage=ceil(($nheigth*100)/$oheight);
		$width_canv=ceil(($owidth*$percentage)/100);
		
		$imagen1 = imagecreate($width_canv, $nheigth);
		imagecopyresized($imagen1, $source, 0, 0, 0, 0, $width_canv, $nheigth, $owidth, $oheight);
		$image_final=$imagen1;
		
		imagedestroy($imagen1);
	}else if($forze=='bouth'){
		add_new_message("Forzar de ambas partes: $owidth - $oheight",'log');
		if($owidth < $oheight){	//imagen vertical
			add_new_message(" 1 : $owidth < $oheight",'log');
			if($nwidth < $nheigth){	
				add_new_message(" 2 : $nwidth < $nheigth ",'log');
				
				$porcentH=($nheigth*100)/$oheight;	
				$width_canv = ($porcentH*$owidth)/100;
				$heigth_canv=$nheigth;
				
				if($width_canv > $nwidth){
					$percentW=($nwidth*100)/$owidth;
					$heigth_canv=ceil(($percentW*$oheight)/100);
					$width_canv = $nwidth;
				}
				if($margin > 0){
					$width_canv = $width_canv - $margin;
					$heigth_canv= $heigth_canv - $margin;
				}
				$imagen2 = imagecreate($width_canv, $heigth_canv);
				//previe_image($imagen2);
				imagecopyresized($imagen2, $source, 0, 0, 0, 0, $width_canv, $heigth_canv, $owidth, $oheight);
				
				if($width_canv!=$nwidth || $heigth_canv!=$nheigth){
					$imagen3 = imagecreate($nwidth, $nheigth);
					$ccolor=hex2rgb("FFFFFF");
					//imagecolorset ( $thumb2, 2, $ccolor['red'], $ccolor['green'], $ccolor['blue'] );
					$colorr = ImageColorAllocate( $imagen3, $ccolor['red'], $ccolor['green'], $ccolor['blue'] ); 
					imagefill($imagen3, 0, 0, $colorr);
					
					$x=@($nwidth-$width_canv)/2;
					$_x=0;
					$y=@($nheigth-$heigth_canv)/2;
					$_y=0;
					if($x>0){
						$_x=$x;
					}
					if($y>0){
						$_y=$y;
					}
					imagecopy ( $imagen3 , $imagen2 , $_x, $_y, 0, 0, $width_canv, $heigth_canv );
					
					$image_final = $imagen3;
					//previe_image($image_final);
					$final3=true;
				}else{
					
					$image_final=$imagen2;
					
					//	previe_image($image_final);
					
				}
			}else{
				add_new_message("No Soportado : Else 2",'log');
			}
		}else{
			//imagen horizontal
			add_new_message("Else 1",'log');
			$porcentH=($nheigth*100)/$oheight;	
			$width_canv = ($porcentH*$owidth)/100;
			$heigth_canv=$nheigth;
			
			if($width_canv > $nwidth){
				$percentW=($nwidth*100)/$owidth;
				$heigth_canv=ceil(($percentW*$oheight)/100);
				$width_canv = $nwidth;
			}
			if($margin > 0){
				$width_canv = $width_canv - $margin;
				$heigth_canv= $heigth_canv - $margin;
			}
			$imagen2 = imagecreate($width_canv, $heigth_canv);
			//previe_image($imagen2);
			imagecopyresized($imagen2, $source, 0, 0, 0, 0, $width_canv, $heigth_canv, $owidth, $oheight);
			//previe_image($imagen2);
			$image_final=$imagen2;
			//fin imagen horizontal
		}
	}
	
	// Guardar imagen y terminar
	add_new_message("prep save: $savepath$image_file",'log');
	
	previe_image($image_final);
	if($image_final && imagejpeg($image_final,$savepath.$image_file,80)){
		imagedestroy($image_final);
		imagedestroy($source);
		if($forze=='bouth'){
			if($final3==true){
				imagedestroy($imagen3);
			}
			imagedestroy($imagen2);
		}
		add_new_message("Imagen guardada",'log');
		error_reporting(E_ERROR | E_WARNING | E_PARSE);
		return true;
	}else{
		
		add_new_message("Error saving $savepath$image_file",'fimage-error');
	}
	error_reporting(E_ERROR | E_WARNING | E_PARSE);
	return false;
}
function previe_image($image){
	header('Content-type: image/jpeg');
	imagejpeg($image);
	imagedestroy($image);
	exit;
}

?>